package cn.szabadsag.hrm.context;

import cn.szabadsag.hrm.domain.LoginUser;
import com.alibaba.fastjson.JSON;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author yangcong
 * @date 2022年04月17日 11:13
 * @since
 */
public class LoginContext {

    public static LoginUser getLogin(){
        try {
            //  获取登录用户
            SecurityContext securityContext = SecurityContextHolder.getContext();
            Authentication authentication = securityContext.getAuthentication();
            if (authentication != null){
                //  匿名用户
                String principal = (String) authentication.getPrincipal();
                LoginUser loginUser = JSON.parseObject(principal, LoginUser.class);
                return loginUser;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
